namespace IMPlant.Core.Web.Control.GridViewFunction
{
    using IMPlant.Core.Web.Control;
    using IMPlant.Core.Web.Control.Helper;
    using System;

    public class CascadeCheckboxFunction : ExtendFunction
    {
        public CascadeCheckboxFunction()
        {
        }

        public CascadeCheckboxFunction(GridView grid) : base(grid)
        {
        }

        private void _grid_PreRender(object sender, EventArgs e)
        {
            string script = "";
            foreach (CascadeCheckbox checkbox in base._grid.CascadeCheckboxes)
            {
                script = script + string.Format("_grid_ccGridView_pre.push('{0}');", Common.GetChildControlPrefix(base._grid));
                script = script + string.Format("_grid_ccAll_post.push('{0}');", checkbox.ParentCheckboxID);
                script = script + string.Format("_grid_ccItem_post.push('{0}');", checkbox.ChildCheckboxID);
            }
            if (!base._grid.Page.ClientScript.IsClientScriptBlockRegistered(string.Format("_grid_CascadeCheckbox_{0}", base._grid.ID)))
            {
                base._grid.Page.ClientScript.RegisterClientScriptBlock(base._grid.GetType(), string.Format("_grid_CascadeCheckbox_{0}", base._grid.ID), script, true);
            }
        }

        protected override void Execute()
        {
            base._grid.PreRender += new EventHandler(this._grid_PreRender);
        }
    }
}

